package ch.uzh.ifi.sopra.gruppe9.rmi_test;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {
	
	private static final long serialVersionUID = -5949539985103045666L;
	
	private static Server instance;
	
	private IGame game;

	private Server() throws RemoteException {
		this.game = new Game();
		
		try {
			LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
		} catch (RemoteException ex) {
			System.out.println(ex.getMessage());
		}
		try {
			Naming.rebind("game", game);
			Naming.rebind("playerOne", game.getPlayerOne());
			Naming.rebind("playerTwo", game.getPlayerTwo());
			
		} catch (MalformedURLException ex) {
			System.out.println(ex.getMessage());
		} catch (RemoteException ex) {
			System.out.println(ex.getMessage());
		}
		System.out.println("Server started");
	}

	public static Server getInstance() throws RemoteException {
		if(instance == null)
			instance = new Server();
		return instance;
	}
	
	public IGame getGame() {
		return game;
	}

}
